<template>
  <div>
    <template>
      <el-breadcrumb separator-class="el-icon-arrow-right">
        <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
        <el-breadcrumb-item>奖励管理</el-breadcrumb-item>
        <el-breadcrumb-item>奖励列表</el-breadcrumb-item>
      </el-breadcrumb>
      <el-card>
        <el-row :gutter="20">
          <el-col :span="6">
            <el-input placeholder="请输入内容" v-model="queryInfo.query" clearable = true @clear="likeClear()">
              <el-button slot="append" icon="el-icon-search" @click="getShopListByLike"></el-button>
            </el-input>
          </el-col>
          <el-col :span="4">
            <el-button type="primary" @click="addTaskVisible = true">新增奖励</el-button>
          </el-col>
        </el-row>
        <!-- 任务列表 -->

        <el-table :data="shopList" style="width: 100%" border stripe>
          <el-table-column type="expand">
            <template slot-scope="props">
              <el-form label-position="left" inline class="demo-table-expand">
                <el-form-item label="名字">
                  <span>{{ props.row.name }}</span>
                </el-form-item>
                <el-form-item label="奖励介绍">
                  <span>{{ props.row.introduce }}</span>
                </el-form-item>
                <el-form-item label="兑换需求等级">
                  <span>{{ props.row.prepositionLevel }}</span>
                </el-form-item>
                <el-form-item label="兑换所需金币">
                  <span>{{ props.row.money}}</span>
                </el-form-item>
                <el-form-item label="可兑换次数">
                  <span>{{ props.row.amount }}</span>
                </el-form-item>
                <el-form-item label="兑现限时">
                  <span>{{ props.row.timeout }} (小时)</span>
                </el-form-item>
              </el-form>
            </template>
          </el-table-column>
          <el-table-column type="index" label="#"></el-table-column>
          <el-table-column label="奖励名称" prop="name"></el-table-column>
          <el-table-column label="剩余兑换次数" prop="amount"></el-table-column>
          <el-table-column label="是否开放兑换">
            <template slot-scope="scope">
              <el-switch
                v-model="scope.row.isShow"
                :active-value="1"
                :inactive-value="0"
                @change="userStateChanged(scope.row)"
              ></el-switch>
            </template>
          </el-table-column>
          <el-table-column label="操作">
            <template slot-scope="scope">
              <el-button
                type="primary"
                icon="el-icon-edit"
                size="mini"
                circle
                @click="showEditDialog(scope.row.id)"
              ></el-button>
              <el-button
                type="danger"
                icon="el-icon-delete"
                size="mini"
                circle
                @click="deleteShop(scope.row.id)"
              ></el-button>
            </template>
          </el-table-column>
        </el-table>

        <el-pagination
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          :current-page="queryInfo.pageNum"
          :page-sizes="[5, 10, 20, 40]"
          :page-size="queryInfo.pageSize"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
        ></el-pagination>
      </el-card>
    </template>

    <el-dialog
      title="新增奖励"
      :visible.sync="addTaskVisible"
      width="50%"
      @close="addDialogClosed"
      :close-on-click-modal="false"
      destroy-on-close="true"
    >
      <el-form
        :model="addShopInfo"
        ref="addTaskFormRef"
        :rules="addUserFormRules"
        label-width="120px"
      >
        <el-form-item label="奖励名" prop="name">
          <el-input v-model="addShopInfo.name"></el-input>
        </el-form-item>
        <el-form-item label="奖励介绍" prop="introduce">
          <el-input type="textarea" v-model="addShopInfo.introduce"></el-input>
        </el-form-item>
        <el-form-item label="承诺完成时间" prop="introduce">
          <el-input-number v-model="addShopInfo.timeout" :max="240"></el-input-number>(小时)
        </el-form-item>
        <el-form-item label="兑换价格" prop="awardLevel">
          <el-slider v-model="addShopInfo.money" show-input max="10000"></el-slider>
        </el-form-item>
        <el-form-item label="可兑换次数" prop="awardMoney">
          <el-slider v-model="addShopInfo.amount" show-input max="200"></el-slider>
        </el-form-item>
        <el-form-item label="兑换需求等级" prop="prepositionLevel">
          <el-slider v-model="addShopInfo.prepositionLevel" show-input></el-slider>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="addTaskVisible = false">取 消</el-button>
        <el-button type="primary" @click="addShop">确 定</el-button>
      </span>
    </el-dialog>
    <!-- 修改 -->
    <el-dialog
      title="修改奖励信息"
      :visible.sync="editDialogVisible"
      width="50%"
      @close="editDialogClosed"
    >
      <el-form :model="updateShopForm" ref="editUserFormRef" label-width="100px">
        <el-form-item label="奖励名" prop="name">
          <el-input v-model="updateShopForm.name"></el-input>
        </el-form-item>
        <el-form-item label="奖励介绍" prop="introduce">
          <el-input type="textarea" v-model="updateShopForm.introduce"></el-input>
        </el-form-item>
        <el-form-item label="承诺完成时间" prop="introduce">
          <el-input-number v-model="updateShopForm.timeout" :max="240"></el-input-number>(小时)
        </el-form-item>
        <el-form-item label="兑换价格" prop="awardLevel">
          <el-slider v-model="updateShopForm.money" show-input max="10000"></el-slider>
        </el-form-item>
        <el-form-item label="可兑换次数" prop="awardMoney">
          <el-slider v-model="updateShopForm.amount" show-input max="200"></el-slider>
        </el-form-item>
        <el-form-item label="兑换需求等级" prop="prepositionLevel">
          <el-slider v-model="updateShopForm.prepositionLevel" show-input></el-slider>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="editDialogVisible = false">取 消</el-button>
        <el-button type="primary" @click="updateShop">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>


<style lang="less" scoped>
.demo-table-expand {
  font-size: 0;
}
.demo-table-expand label {
  width: 90px;
  color: #99a9bf;
}
.demo-table-expand .el-form-item {
  margin-right: 0;
  margin-bottom: 0;
  width: 50%;
}
</style>

<script>
import {
  getSupervisorShopId,
  updateShop,
  addShop,
  getSupervisorShopList,
} from "@/utils/SupervisorApi";
export default {
  data() {
    return {
      queryInfo: {
        query: "",
        // 当前页数
        pageNum: 1,
        // 每页显示多少数据
        pageSize: 5,
      },
      // 修改用户
      editDialogVisible: false,
      //增加商品
      addTaskVisible: false,
      updateShopForm: {
        id: "",
        name: "",
        introduce: "",
        prepositionLevel: "",
        timeout: "10",
        money: "",
        amount: "",
        url: "",
      },
      addShopInfo: {
        id: "",
        name: "",
        introduce: "",
        prepositionLevel: "",
        timeout: 10,
        money: "",
        amount: "",
        url: "",
      },
      shopList: [],
      total: 0,
    };
  },
  created() {
    this.getShopList();
  },
  methods: {
    likeClear(){
      this.queryInfo.pageNum = 1;
      this.getShopList()
    },
    getShopListByLike(){
      this.queryInfo.pageNum = 1
      this.getShopList()
    },
    // 点击修改按钮之后 需要一个查询然后赋值 打开显示开关
    async showEditDialog(id) {
      const { data: res } = await getSupervisorShopId({
        id: id,
      });
      if (res.meta.status !== 200) {
        return this.$message.error("获取奖励失败！");
      }
      this.updateShopForm = res.data;
      this.editDialogVisible = true;
    },
    //关闭修改奖励
    editDialogClosed() {
      this.$refs.editUserFormRef.resetFields();
      this.editDialogVisible = false;
    },
    // 监听 switch开关 状态改变
    async userStateChanged(row) {
      console.log(row.isShow);
      console.log(row);
      const { data: res } = await updateShop({
        id: row.id,
        isShow: row.isShow,
      });
      if (res.meta.status !== 200) {
        userInfo.mg_state = !userInfo.mg_state;
        return this.$message.error("更新用户状态失败");
      }
      this.$message.success("更新用户状态成功！");
    },
    // 删除
    async deleteShop(id) {
      const confirmResult = await this.$confirm(
        "此操作将永久删除该奖励, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);
      // 点击确定 返回值为：confirm
      // 点击取消 返回值为： cancel
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }
      const { data: res } = await updateShop({
        id: id,
        isDelete: 1,
      });
      if (res.meta.status !== 200) {
        this.$message.error("删除奖励失败！");
        this.getShopList();
        return;
      }
      this.$message.success("删除奖励成功！");
      this.getShopList();
    },
    // 修改奖励
    async updateShop() {
      if (this.updateShopForm.name == null || this.updateShopForm.name == "") {
        this.$message.error("名字不能为空");
        retrun;
      }
      const { data: res } = await updateShop(this.updateShopForm);
      if (res.meta.status !== 200) {
        this.$message.error("修改奖励失败！");
        this.getShopList();
        return;
      }
      this.$message.success("修改奖励成功！");
      this.editDialogVisible = false;
      this.getShopList();
    },
    // 添加用户
    async addShop() {
      // 提交请求前，表单预验证
      if (this.addShopInfo.name == null || this.addShopInfo.name == "") {
        this.$message.error("名字不能为空");
        retrun;
      }
      const { data: res } = await addShop(this.addShopInfo);
      if (res.meta.status !== 200) {
        this.$message.error("添加奖励失败！");
        return;
      }
      this.$message.success("添加奖励成功！");
      // 隐藏添加任务对话框
      this.addTaskVisible = false;
      this.getShopList();
    },
    addDialogClosed() {
      this.$refs.addTaskFormRef.resetFields();
    },
    async getShopList() {
      console.log("dasd");
      const { data: res } = await getSupervisorShopList(this.queryInfo);
      if (res.meta.status !== 200) {
        return this.$message.error("获取奖励列表失败！");
      }
      this.shopList = res.data.list;
      this.total = res.data.total;
    },
    handleSizeChange(newSize) {
      this.queryInfo.pageSize = newSize;
      this.getShopList();
    },
    // 监听 页码值 改变事件
    handleCurrentChange(newSize) {
      this.queryInfo.pageNum = newSize;
      this.getShopList();
    },
  },
};
</script>